//
//  API.swift
//  Link
//
//  Created by melvyn on 2018/11/25.
//

import Foundation
import Alamofire

public typealias BasicAuthorization = (username: String, password: String)
public protocol API {
    
    /// Determind whether the user have logged in
    ///
    /// - Returns: result value
    var isLoggedIn: Bool { get }
    
    
    /// Request user associated with id `uid`.
    ///
    /// - Parameters:
    ///   - uid: userID
    ///   - completionHandler: Response callback
    /// - Returns: Task for request
    func requestUser(_ uid: User.ID, completionHandler: @escaping (Result<User>) -> Swift.Void) -> DataRequest
    
    
    /// Open a new Live
    ///
    /// - Parameters:
    ///   - multipartForm: Live info descriptor
    ///   - completionHandler: Response callback
    func openLiveStream(_ multipartForm: LiveStream.MultipartForm, completionHandler: @escaping (Result<LiveStream>) -> Swift.Void)
    
    
    /// Shutdown a in progress Live stream
    ///
    /// - Parameters:
    ///   - liveStream: A live stream that is in progress
    ///   - completionHandler: Response callback
    /// - Returns: Task for request
    func shutdownLiveStream(_ liveStream: LiveStream, completionHandler: @escaping (Result<Bool>) -> Swift.Void) -> DataRequest
    
    
    /// Request all Live streams that is running in progress
    ///
    /// - Parameters:
    ///   - completionHandler: Response callback
    /// - Returns: Task for request
    func requestLiveStreams(completionHandler: @escaping (Result<[LiveStream]>) -> Swift.Void) -> DataRequest
    
    
    /// Request a in progress Live stream
    ///
    /// - Parameters:
    ///   - liveStream: stream in for descriptor
    ///   - completionHandler: Response callback
    /// - Returns: Task for request
    func showLiveStream(_ liveStream: LiveStream, completionHandler: @escaping (Result<LiveStream>) -> Swift.Void) -> DataRequest

    
    /// Attempt to Login
    ///
    /// - Parameters:
    ///   - input: Basic authorization info
    ///   - completionHandler: Response callback
    /// - Returns: Task for request
    func attemptAppLogin(_ input: BasicAuthorization, completionHandler: @escaping (Result<AuthSession>) -> Swift.Void) -> DataRequest

    
    /// Attempt to Registration
    ///
    /// - Parameters:
    ///   - input: Registration info
    ///   - completionHandler: Response callback
    /// - Returns: Task for request
    func attemptAppRegistration(_ input: User.MultipartForm, completionHandler: @escaping (Result<Bool>) -> Swift.Void) -> DataRequest

    
    /// Attempt to Logout
    ///
    /// - Parameter completionHandler: Response callback
    /// - Returns: Task for request
    func attemptAppLogout(completionHandler: @escaping (Result<AuthSession.ID>) -> Void) -> DataRequest
}


#if canImport(RxSwift)
import RxSwift

protocol RxAPI {
    
    func requestUser(_ uid: User.ID) -> Observable<Result<User>>
    
    
    func openLiveStream(_ multipartForm: LiveStream.MultipartForm) -> Observable<Result<LiveStream>>

    
    func shutdownLiveStream(_ liveStream: LiveStream) -> Observable<Result<Bool>>
    
    
    func requestLiveStreams() -> Observable<Result<[LiveStream]>>

    
    func showLiveStream(_ liveStream: LiveStream) -> Observable<Result<LiveStream>>
    
    
    func attemptAppRegistration(_ input: User.MultipartForm) -> Observable<Result<Bool>>
    
    
    func attemptAppLogin(_ input: BasicAuthorization) -> Observable<Result<AuthSession>>
    

    func attemptAppLogout() -> Observable<Result<AuthSession.ID>>
}
#endif
